<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange" @sendUserSearch="getSearch" :searchData="userSearchData" :isHaveExport="false" :isHaveAdd="false">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="loading" header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%">
          <el-table-column label="会员" min-width="100" align="center">
            <template slot-scope="scope">
              <div class="tc">
                <img style="width:50px;height:50px" :src="$fnc.getImgUrl(scope.row.avatar)" class="user_img" />
                <span>{{ scope.row.rating_cn }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="会员信息" min-width="160">
            <template slot-scope="scope">
              <div class="user_table">

                <div>
                  <span>用户昵称：</span>
                  <small>{{ scope.row.nickname || "----" }}</small>
                </div>
                <div>
                  <span>会员账号：</span>
                  <small>{{ scope.row.username }}</small>
                </div>
                <div>
                  <span>注册时间：</span>
                  <small>{{ scope.row.created_time | getTimeFormat }}</small>
                </div>
                <div>
                  <span>代理级别：</span>
                  <el-tag v-if="scope.row.is_agent == 3" size="mini" type="primary" effect="dark" style="color:#fff">省代</el-tag>
                  <el-tag v-else-if="scope.row.is_agent == 2" size="mini" type="danger" effect="dark" style="color:#fff">市代</el-tag>
                  <el-tag v-else-if="scope.row.is_agent == 1" size="mini" type="success" effect="dark" style="color:#fff">区代</el-tag>
                  <el-tag v-else-if="scope.row.is_agent == 4" size="mini" type="warning" effect="dark" style="color:#fff">乡镇</el-tag>
                  <span v-else>---</span>
                </div>
                <div>
                  <span>代理区域：</span>
                  <small>{{ `${scope.row.agent_province || ''}${scope.row.agent_city || ''}${scope.row.agent_area || ''}${scope.row.agent_town || ''}`  || '---'}}</small>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="下级会员数" min-width="150">
            <template slot-scope="scope">
              <div class="user_table">
                <div>
                  <span>直推/分销：</span>
                  <small>{{ scope.row.ynumber }} / {{ scope.row.yvip }}</small>
                </div>

                <div>
                  <span>累计消费：</span>
                  <small>{{ scope.row.buy | toFixedNum }}</small>
                </div>
                <div>
                  <span>累计佣金：</span>
                  <small>{{ scope.row.sum_amount | toFixedNum }}</small>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="商务数据" min-width="150">
            <template slot-scope="scope">
              <div class="user_table">
                <div>
                  <span>订单数：</span>
                  <small>{{ scope.row.s_order_num }}</small>
                </div>
                <div>
                  <span>销售额：</span>
                  <small>{{ scope.row.s_sales | toFixedNum }}</small>
                </div>
                <div>
                  <span>已结算：</span>
                  <small>{{ scope.row.s_settled | toFixedNum }}</small>
                </div>
                <div>
                  <span>未结算：</span>
                  <small>{{ scope.row.s_unsettled | toFixedNum }}</small>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="200">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="getUserInfo(scope.row.id)" type="primary">详情</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" @click="editAgent(scope.row)" type="primary">编辑</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" title="代理商" :modal-append-to-body="false" :visible.sync="showAgent" class="tl size_13" width="800px">
      <el-form :model="agent" ref="data" label-width="120px" v-if="showAgent">
        <el-form-item label="代理商" prop="username">
          <el-col :span="24">
            <el-radio v-model="agent.is_agent" @change="changeRadio" label="0">不是</el-radio>
            <el-radio v-model="agent.is_agent" @change="changeRadio" label="4">乡镇</el-radio>
            <el-radio v-model="agent.is_agent" @change="changeRadio" label="1">区代</el-radio>
            <el-radio v-model="agent.is_agent" @change="changeRadio" label="2">市代</el-radio>
            <el-radio v-model="agent.is_agent" @change="changeRadio" label="3">省代</el-radio>
          </el-col>
        </el-form-item>

        <el-form-item label="代理地区" prop>
          <el-col :span="12">
            <city-cascader v-if="showCity && agent.is_agent > 0" :placeholder="selectedOptions.join('-')" :level="
                agent.is_agent == 4
                  ? 3
                  : agent.is_agent == 3
                  ? 0
                  : agent.is_agent == 2
                  ? 1
                  : agent.is_agent == 1
                  ? 2
                  : 0
              " @change="handleChange" :selected="selectedOptions" />
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="subAgent">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import cityCascader from "@/components/currency/cityCascader/cityCascader";
import { userAgentSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: userAgentSearchData,
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      arr: [],
      searchAll: {},
      row: {},
      selectedOptions: [],
      showAgent: false,
      options: this.$addressJson,
      optionProps: {
        value: "y",
        label: "t",
        children: "z",
        checkStrictly: true,
      },
      agent: {
        is_agent: "0",
        agent_province: "",
        agent_city: "",
        agent_area: "",
        agent_town: "",
      },
      showCity: true,
    };
  },
  components: {
    cityCascader,
  },
  methods: {
    changeRadio() {
      this.showCity = false;
      setTimeout(() => {
        this.showCity = true;
      }, 500);
    },
    editAgent(item) {
      this.showAgent = true;
      this.agent.id = item.id;
      this.agent.is_agent = item.is_agent;
      this.agent.agent_province = item.agent_province;
      this.agent.agent_city = item.agent_city;
      this.agent.agent_area = item.agent_area;
      this.agent.agent_town = item.agent_town;
      this.$nextTick(() => {
        this.selectedOptions = [
          item.agent_province,
          item.agent_city,
          item.agent_area,
        ];

        if (item.agent_town) {
          this.selectedOptions.push(item.agent_town);
        }
      });
    },
    subAgent() {
      this.showAgent = false;
      var params = this.agent || {};

      this.$api.getUser.editAgent(params).then((res) => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("操作成功");
          this.getNewsAll(this.searchAll);
        }
      });
    },
    handleChange(value) {
      this.agent.agent_province = "";
      this.agent.agent_city = "";
      this.agent.agent_area = "";
      this.agent.agent_town = "";
      if (value) {
        for (var i = 0; i < value.length; i++) {
          if (i == 0) this.agent.agent_province = value[i];
          if (i == 1) this.agent.agent_city = value[i];
          if (i == 2) this.agent.agent_area = value[i];
          if (i == 3) this.agent.agent_town = value[i];
        }
      }
      console.log(this.agent);
    },
    getOpenThis() {
      this.show_supplier = false;
    },
    getUserInfo(id) {
      this.$store.commit("addbreadcrumbs", {
        title: "用户详情",
        url: `/index/user/user_info?id=${id}`,
      });
      this.$router.push({
        path: "/index/user/user_info",
        query: { id: id },
      });
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;

      this.$api.getAgent.getAgentList(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
          console.log(res.data.result);
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getUser.userDel(data).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");

              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    reload() {
      this.getNewsAll(this.searchAll);
    },
  },
  beforeCreate() {},
  created() {
    this.getNewsAll();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
